import 'dart:io';
import 'dart:isolate';

/// CreateTime 2020/6/9 13:16
/// CreateUser chen 
/// ProjectName flutter_elf
import 'package:flutter_elf/config/component_index.dart';

import 'file_utils.dart';
//https://medium.com/flutter-community/parsing-html-in-dart-with-html-package-cd43c29cc460
//下载煎蛋妹子图片
void main()async{

  String path = 'test/download/cache2.txt';
  List<String> datas = File(path).readAsStringSync().split('\n');
//  print(datas);
//  List<GankGirlData> datas = []..addAll(g1.data)..addAll(g2.data);

  count = datas.length;
  rcvPort = ReceivePort();
  download(datas);

  rcvPort.listen((msg){
    print('开始下个任务$index       $msg');
    download(datas);
  });
  await Future.delayed(Duration(seconds: 100000));
}
int index = 0;
int count;
ReceivePort rcvPort;
download(List<String> url)async{
  print('开始下载' + url[index]);
  if(!File(url[index]).existsSync()){
    print('不存在，存储');
    await FileUtils.downloadFileByUrl(url[index], 'E:\\测试3\\${getName(url[index])}.jpg');
  }
  print('下载完成');
  index++;
  Isolate iso = await Isolate.spawn(port, rcvPort.sendPort);
  print(iso);
  print(iso);
}

port(SendPort sendPort){
  sendPort.send('1232');
}

down(List<String> items){
  List<Future> fs = items.map((i)async{
    return await FileUtils.downloadFileByUrl(i, 'D:\\测试\\${getName(i)}.jpg');
  }).toList();
  Future.wait(fs).then((value){
    print(value.toString());
  });
}

String getName(String str){
  return str.substring(str.lastIndexOf('/')+1);
}